import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  /* {
    path: "/user",
    redirect: "/user/account"  //当用户访问"/"的时候,自动访问homeShopping
  }, */
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/goods',
    component: () => import(/* webpackChunkName: "goods" */ '../views/Goods.vue')
  },
  {
    path: '/user',
    redirect: "/user/cart",
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
    children: [
      {
        path: '/user/cart',
        component: () => import('../components/user/Cart.vue')
      },
      /* {
        path: '/user/shopping',
        component: () => import('../components/Shopping.vue')
      } */
    ]
  }
  ,
  {
    path: '/order',
    component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue')
  }
  ,
  {
    path: '/free',
    component: () => import(/* webpackChunkName: "free" */ '../views/Free.vue')
  },
  {
    path: '/details',
    component: () => import(/* webpackChunkName: "details" */ '../views/Details.vue')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 把这段代码直接粘贴到router/index.js中的Vue.use(VueRouter)之前
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};

/* router.beforeEach((to, from, next) => {
  //跳转每一个路由之前,都需要在这里执行

  console.log(`参${from.path}跳转到${to.path}`);

  // 业务需要:没登录不能进去个人中心
  if (to.path === "/user/account") {
    let token = localStorage.getItem("x-auth-token")
    if (token) {
      //登录就放行
      next()
    }else{
      //没登录,就提示
      store.dispatch("showToast/asyncChenIsShowToast", {
        msg: "请先登录",
        type: "warning"
      })     
      return
    }
    return
  }
  next()
}) */
export default router
